﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;

namespace Quản_Lý_Thư_Viện
{
    public partial class frmNhapKho : Form
    {
        public frmNhapKho()
        {
            InitializeComponent();
        }

        PhieunhapsachControl control = new PhieunhapsachControl();
        ctpnsControl ctcontrol = new ctpnsControl();
        DausachControl dscontrol = new DausachControl();
        ArrayList dsdausach;
        bool kt = false;
        bool bInserted = false;
        public static string timpns;

        private void frmNhapKho_Load(object sender, EventArgs e)
        {
            dgvNhapKho.DataSource = control.laydspns();
            dsdausach = dscontrol.getdsmadausach();
            cboMaDauSach.DisplayMember = "TENSACH";
            cboMaDauSach.ValueMember = "MADAUSACH";
            cboMaDauSach.DataSource = dsdausach;           
        }
        private void laythongtinpns(ref PhieunhapsachInfo info)
        {
            info.Mapns = txtMaPhieuNhap.Text;
            info.Ngnhap = dtpNgayNhap.Value;
            info.Tongsoluong = Convert.ToInt32(txtSoLuong.Text);
            info.Trigiapns = Convert.ToInt32(txtTongGiaTri.Text);
            info.Madausach = cboMaDauSach.SelectedValue.ToString();
            if (txtSLTungDauSach.Text == "") 
                info.Soluong = 0;
            else
                info.Soluong = Convert.ToInt32(txtSLTungDauSach.Text);
        }

        //private void enable()
        //{
        //    txtMaPhieuNhap.Enabled = true;
        //    txtSoLuong.Enabled = true;
        //    txtTongGiaTri.Enabled = true;
        //    dtpNgayNhap.Enabled = true;
        //}
        private void ShowControl(bool bShow)
        {
            //txtMaPhieuNhap.Enabled = bInserted;          
            txtSoLuong.Enabled = bShow;
            txtTongGiaTri.Enabled = bShow;           
            dtpNgayNhap.Enabled = bShow;
            btnSave.Enabled = bShow;
            
        }
        private void Clear()
        {
            txtMaPhieuNhap.Text = "";
            txtSoLuong.Text = "";
            txtTongGiaTri.Text = "";
            txtSLTungDauSach.Text = "";
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            bInserted = true;
            Clear();
            ShowControl(true);
            txtSLTungDauSach.Enabled = false;
            cboMaDauSach.Enabled = false;
            btnThem.Enabled = false;
            btnSua.Enabled = false;
            int i = dgvNhapKho.Rows.Count + 1;
            txtMaPhieuNhap.Text = "PN" + i + "";
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            PhieunhapsachInfo info = new PhieunhapsachInfo();
            if (
                    txtSoLuong.Text == "" ||
                    txtTongGiaTri.Text == ""
                    )
            {
                MessageBox.Show("Chưa Nhập Đầy Đủ Các Thành Phần Yêu Cầu !", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                if (bInserted)
                {


                    laythongtinpns(ref info);
                    if (kiemtra(txtMaPhieuNhap.Text.Trim()) == true)
                    {
                        MessageBox.Show("Trùng Mã Phiếu Nhập Kho");
                    }
                    else
                    {
                        control.insertphieunhapsach(info);
                        MessageBox.Show("Thêm Thành Công");
                        dgvNhapKho.DataSource = control.laydspns();
                        bInserted = false;
                        ShowControl(false);
                        txtSLTungDauSach.Enabled = true;
                        cboMaDauSach.Enabled = true;
                        btnThem.Enabled = true;
                        btnSua.Enabled = true;                     
                        btnNhap.Enabled = true;
                    }
                }

                else
                {

                    laythongtinpns(ref info);
                    control.updatephieunhapsach(info);
                    MessageBox.Show("Sửa Thành Công");
                    dgvNhapKho.DataSource = control.laydspns();
                    ShowControl(false);
                    btnThem.Enabled = true;
                    btnSua.Enabled = true;                  
                }
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            PhieunhapsachInfo info = new PhieunhapsachInfo();
            laythongtinpns(ref info);
            ctcontrol.deletectpns2(info);
            control.deletephieunhapsach(info);
            MessageBox.Show("Xóa Thành Công");
            dgvNhapKho.DataSource = control.laydspns();
        }

        private void dgvNhapKho_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvNhapKho.RowCount > 0)
            {
                txtMaPhieuNhap.Text = dgvNhapKho.CurrentRow.Cells["MAPNS"].Value.ToString();
                txtSoLuong.Text = dgvNhapKho.CurrentRow.Cells["TONGSOLUONG"].Value.ToString();
                txtTongGiaTri.Text = dgvNhapKho.CurrentRow.Cells["TRIGIAPNS"].Value.ToString();
                dtpNgayNhap.Text = dgvNhapKho.CurrentRow.Cells["NGNHAP"].Value.ToString();
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            bInserted = false;
            ShowControl(true);
            txtSLTungDauSach.Enabled = false;
            cboMaDauSach.Enabled = false;         
            btnThem.Enabled = false;
            btnSua.Enabled = false;
            
        }

        private void btnNhap_Click(object sender, EventArgs e)
        {
            PhieunhapsachInfo info = new PhieunhapsachInfo();
            
            if (   txtMaPhieuNhap.Text == "" ||
                   txtSoLuong.Text == "" ||
                   txtTongGiaTri.Text == "" ||
                   txtSLTungDauSach.Text == ""
                   )
            {
                MessageBox.Show("Chưa Nhập Đầy Đủ Các Thành Phần Yêu Cầu !", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                laythongtinpns(ref info);
                ctcontrol.insertctpns(info);
                MessageBox.Show("Đã Thêm 1 Chi Tiết Phiếu Nhập Vào Database");
            }    

        }
        private Boolean kiemtra(string ma)
        {

            for (int j = 0; j < dgvNhapKho.RowCount ; j++)
            {
                if (ma == dgvNhapKho.Rows[j].Cells[0].Value.ToString())//cells[chỉ số so sánh]
                {
                    kt = true;
                    break;
                }
                else
                    kt = false;
            }
            return kt;
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            if (dgvNhapKho.RowCount > 0)
            {
                txtMaPhieuNhap.Text = dgvNhapKho.CurrentRow.Cells["MAPNS"].Value.ToString();
                txtSoLuong.Text = dgvNhapKho.CurrentRow.Cells["TONGSOLUONG"].Value.ToString();
                txtTongGiaTri.Text = dgvNhapKho.CurrentRow.Cells["TRIGIAPNS"].Value.ToString();
                dtpNgayNhap.Text = dgvNhapKho.CurrentRow.Cells["NGNHAP"].Value.ToString();
            }
            bInserted = false;
            ShowControl(false);
            txtSLTungDauSach.Enabled = false;
            cboMaDauSach.Enabled = false;
            btnSua.Enabled = true;
            btnThem.Enabled = true;
            btnNhap.Enabled = false;

        }

        private void KiemTraNhapSo_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
                MessageBox.Show("Yêu Cầu Nhập Số");
            }
        }
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == (Keys.Escape))
            {
                this.Close();
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            timpns = txtMaPhieuNhap.Text;
            control.timpns();
        }      

    }
}
